//
//  XLMessageDTVC.m
//  industry
//
//  Created by xl on 2020/12/17.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLMessageDTVC.h"
#import "XLMessageModel.h"
#import "XLDateTool.h"

@interface XLMessageDTVC ()

@property (nonatomic, strong) XLLabel *bodyLB;
@property (nonatomic, strong) XLLabel *timeLB;

@end

@implementation XLMessageDTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"系统通知";
    [self buildView];
}

- (void)buildView {
    [self.view addSubview:self.bodyLB];
    [self.view addSubview:self.timeLB];
    [_bodyLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.top.equalTo(self.view).offset(15);
        make.right.lessThanOrEqualTo(self.view).offset(-30);
    }];
    
    [_timeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bodyLB);
        make.top.equalTo(self.bodyLB.mas_bottom).offset(20);
    }];
    
    _bodyLB.text = self.model.body;
    _timeLB.text = [XLDateTool getDateWithTimeStamp:self.model.gmtCreated formatter:@"yyyy.MM.dd"];
    
    if (self.isRead == NO) {
        [self requestAlreadyReadMessages:@[self.model]];
    }
    
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if (!self.isRead && self.readBlock) {
        self.readBlock();
    }
}

#pragma mark - Net
- (void)requestAlreadyReadMessages:(NSArray *)arr {
    NSMutableArray *temArr = [NSMutableArray array];
    for (XLMessageModel *model in arr) {
        if (!ISEmptyNumber(model.messageId)) {
            [temArr addObject:model.messageId.stringValue];
        }
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (temArr.count > 0) {
        NSString *msgIds = [temArr componentsJoinedByString:@","];
        [params setValue:msgIds forKey:@"msgIds"];
    }
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    [request requestWithMethod:XLHttpMedthTypePOST url:XLOperationReadMessageUrl params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {

    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {

    }];
}

#pragma mark - Setter/getter
- (XLLabel *)bodyLB {
    if (!_bodyLB) {
        _bodyLB = [XLLabel new];
        _bodyLB.font = [UIFont fontOfPingFangSCRegularWithSize:12];
        _bodyLB.textColor = [UIColor blackFontColor];
        _bodyLB.numberOfLines = 0;
    }
    return _bodyLB;
}

- (XLLabel *)timeLB {
    if (!_timeLB) {
        _timeLB = [XLLabel new];
        _timeLB.textColor = [UIColor graySecondFontColor];
        _timeLB.font = [UIFont fontOfPingFangSCRegularWithSize:12];
    }
    return _timeLB;
}

@end
